#!/usr/bin/python2.7
# -*-  encoding: utf-8 -*-

from addon_manager.addon_exception_code import (
    EXCEPTION_SERVICE_NUM_CONSTRAINT,
    EXCEPTION_SERVICE_NO_SUCH_ADDON_SERVICE,
    EXCEPTION_SERVICE_GET_USEABLE_PORT_LIST_FAILED,
    EXCEPTION_SERVICE_ALLOCATE_PORT_FAILED,
    EXCEPTION_SERVICE_OPERATE_RUNNING_ADDON,
    EXCEPTION_SERVICE_OPERATE_STOPPED_ADDON,
    EXCEPTION_SERVICE_NO_SUC_ADDON_CTRL_CMD,
    EXCEPTION_SERVICE_BUILTINADDON_PORT_DUPLICATED,
    EXCEPTION_SERVICE_BUILTINADDON_OP_FAILED,
    EXCEPTION_SERVICE_OPERATE_INVALID_ADDON,
    EXCEPTION_SERVICE_CONVERT_FAILED,
    EXCEPTION_SERVICE_CONVERT_JSON_READ_FAILED,
    EXCEPTION_SERVICE_CONVERT_CHECK_FAILED,
    EXCEPTION_SERVICE_CONFIG_FILE_INVALID,
    EXCEPTION_SERVICE_CONFIG_FILE_MISS_REQUIRED,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_LANGUAGE,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_TYPE,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_SERVICE,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_NAME,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_DESCRIPTION,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_URL,
    EXCEPTION_SERVICE_CONFIG_FILE_ERR_CONVENTION,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_NOT_EXIST,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_MOVE_FAILED,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_UNCOMPRESS_FAILED,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_THREAD_IS_BUSY,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_NO_INSTALL_TASK,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_COMPRESS_FAILED,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_TIMEOUT,
    EXCEPTION_SERVICE_INSTALL_PACKAGE_WRONG_FORMAT,
)


class AddOnBaseException(Exception):

    def __init__(self, err_code, err_msg, *args):
        Exception.__init__(self, err_code, err_msg, *args)

    def msg(self):
        """
        exception info
        """
        return "[ADDON_M]" + self[1]

    def code(self):
        """
        exception code
        """
        return self[0]


class AddOnServiceNumConstraint(AddOnBaseException):

    def __init__(self):
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_NUM_CONSTRAINT, "AddOn service numbers can't be more than 5")


class NoSuchAddOnService(AddOnBaseException):

    def __init__(self):
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_NO_SUCH_ADDON_SERVICE, "AddOn service doesn't exist")


class GetUseablePortListFailed(AddOnBaseException):

    def __init__(self):
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_GET_USEABLE_PORT_LIST_FAILED, "AddOn service get useable port failed")


class AllocatePortFailed(AddOnBaseException):

    def __init__(self):
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_ALLOCATE_PORT_FAILED, "AddOn service allocate port failed")


class RunningAddOnOpException(AddOnBaseException):

    def __init__(self):
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_OPERATE_RUNNING_ADDON, "running addon service cannot be operated.")


class StoppedAddOnOpException(AddOnBaseException):

    def __init__(self):
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_OPERATE_STOPPED_ADDON, "running addon service cannot be operated.")


class NoSuchAddOnCtrlCmd(AddOnBaseException):

    def __init__(self, cmd):
        err_msg = "cmd {} is not supported.".format(cmd)
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_NO_SUC_ADDON_CTRL_CMD, err_msg)


class BuiltinAddOnDuplicatedPort(AddOnBaseException):

    def __init__(self, port):
        err_msg = "port {} is duplicated in whitelist.".format(cmd)
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_BUILTINADDON_PORT_DUPLICATED, err_msg)


class BuiltinAddOnOperateFailed(AddOnBaseException):

    def __init__(self, addon_name, operation):
        err_msg = "built-in addon {} should not be {}.".format(addon_name, operation)
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_BUILTINADDON_OP_FAILED, err_msg)


class InvalidAddOnOperateFailed(AddOnBaseException):

    def __init__(self, addon_name, operation):
        err_msg = "invalid addon {} should not be {}.".format(addon_name, operation)
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_OPERATE_INVALID_ADDON, err_msg)


class ConvertAddOnFailed(AddOnBaseException):

    def __init__(self):
        err_msg = "convert addon failed."
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_CONVERT_FAILED, err_msg)


class ConvertAddonReadJsonFaild(AddOnBaseException):

    def __init__(self):
        err_msg = "read json faild while converting addon."
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_CONVERT_JSON_READ_FAILED, err_msg)


class ConvertAddonCheckFaild(AddOnBaseException):

    def __init__(self):
        err_msg = "check addon faild while converting addon."
        AddOnBaseException.__init__(self, EXCEPTION_SERVICE_CONVERT_CHECK_FAILED, err_msg)


class AddOnConfigBaseException(AddOnBaseException):

    def __init__(self, err_code, err_msg, *args):
        err_msg = "AddOn Config Failed: {}".format(err_msg)
        AddOnBaseException.__init__(self, err_code, err_msg, *args)


class InvalidAddonConfigFile(AddOnConfigBaseException):

    def __init__(self):
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_INVALID, "AddOn service config file is invalid")


class UnknownAddonConvention(AddOnConfigBaseException):

    def __init__(self, convention):
        err_msg = 'AddOn service config file convention is invalid "{}"'.format(convention)
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_CONVENTION, err_msg)


class AddOnConfigRequiredOptionLacked(AddOnConfigBaseException):
    """
    配置文件缺少必须的配置项时引发的异常
    """

    def __init__(self, option):
        err_msg = "AddOn serivce config required option missed ({})".format(option)
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_MISS_REQUIRED, err_msg)


class AddOnConfigErrLanguageType(AddOnConfigBaseException):
    """
    AddOn编程语言不支持
    """

    def __init__(self, language):
        AddOnConfigBaseException.__init__(
            self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_LANGUAGE, 'AddOn serivce language type is not supported "{}"'.format(language)
        )


class AddOnConfigErrType(AddOnConfigBaseException):
    """
    AddOn type不支持
    """

    def __init__(self):
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_TYPE, "AddOn serivce type is not supported")


class AddOnConfigErrService(AddOnConfigBaseException):

    def __init__(self):
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_SERVICE, "AddOn serivce is invalid.")


class AddOnConfigErrName(AddOnConfigBaseException):

    def __init__(self):
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_NAME, "AddOn serivce name is invalid.")


class AddOnConfigErrDescription(AddOnConfigBaseException):

    def __init__(self):
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_DESCRIPTION, "AddOn serivce decription is invalid.")


class AddOnConfigErrUrl(AddOnConfigBaseException):

    def __init__(self):
        AddOnConfigBaseException.__init__(self, EXCEPTION_SERVICE_CONFIG_FILE_ERR_URL, "AddOn serivce url is invalid.")


class AddOnInstallException(AddOnBaseException):

    def __init__(self, err_code, err_msg, *args):
        err_msg = "Install Failed: {}".format(err_msg)
        AddOnBaseException.__init__(self, err_code, err_msg, *args)


class AddOnInstallPackageNotExist(AddOnInstallException):

    def __init__(self):
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_NOT_EXIST, "AddOn install package doesn't exist")


class AddOnInstallPackageMoveFailed(AddOnInstallException):

    def __init__(self, path):
        err_msg = "mv {} failed".format(path)
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_MOVE_FAILED, err_msg)


class AddOnInstallPackageUncompressFailed(AddOnInstallException):

    def __init__(self, path):
        err_msg = "uncompress {} failed".format(path)
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_UNCOMPRESS_FAILED, err_msg)


class AddOnInstallPackageBusyInstaller(AddOnInstallException):

    def __init__(self, _path=None):
        err_msg = "installation thread is busy."
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_THREAD_IS_BUSY, err_msg)


class AddOnInstallPackageNoInstallTask(AddOnInstallException):

    def __init__(self):
        err_msg = "addon installation has not not executed."
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_NO_INSTALL_TASK, err_msg)


class AddOnInstallPackageCompressFailed(AddOnInstallException):

    def __init__(self, path):
        err_msg = "compress {} failed".format(path)
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_COMPRESS_FAILED, err_msg)


class AddOnInstallPackageTimeout(AddOnInstallException):

    def __init__(self, err_msg):
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_TIMEOUT, err_msg)


class AddOnInstallPackageWrongFormat(AddOnInstallException):

    def __init__(self, err_msg):
        AddOnInstallException.__init__(self, EXCEPTION_SERVICE_INSTALL_PACKAGE_WRONG_FORMAT, err_msg)


__all__ = [
    # service
    "AddOnBaseException",
    "AddOnServiceNumConstraint",
    "NoSuchAddOnService",
    "GetUseablePortListFailed",
    "AllocatePortFailed",
    "RunningAddOnOpException",
    "StoppedAddOnOpException",
    "NoSuchAddOnCtrlCmd",
    "BuiltinAddOnDuplicatedPort",
    "BuiltinAddOnOperateFailed",
    "InvalidAddOnOperateFailed",
    "ConvertAddOnFailed",
    "ConvertAddonReadJsonFaild",
    "ConvertAddonCheckFaild",
    "AddOnConfigRequiredOptionLacked",
    "AddOnConfigErrLanguageType",
    "AddOnConfigErrType",
    "AddOnConfigErrService",
    "AddOnConfigErrName",
    "AddOnConfigErrDescription",
    "AddOnConfigErrUrl",
    # addon config exception
    "AddOnConfigBaseException",
    "InvalidAddonConfigFile",
    "AddOnInstallPackageNotExist",
    "AddOnInstallPackageMoveFailed",
    "AddOnInstallPackageUncompressFailed",
    "AddOnInstallPackageBusyInstaller",
    "AddOnInstallPackageNoInstallTask",
    "AddOnInstallPackageCompressFailed",
    "AddOnInstallPackageTimeout",
    "AddOnInstallPackageWrongFormat",
]

if __name__ == "__main__":

    # def TestRaiseAddOnServiceNumConstraint():
    def test_raise_addon_service_num_constraint():
        try:
            raise AddOnServiceNumConstraint()
        except AddOnBaseException as eaddon:
            print("e", eaddon.msg())
            print("err_code", eaddon.code())
